package pathways.Database.Implementation;

/**
 * Title:        Pathways
 * Description:  Pure Java version of the Pathways Project.
 * Copyright:    Copyright (c) 2002
 * Company:      Ants Technologies
 * @author Veejay Sani
 * @version 1.0
 */
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import pathways.Database.Interfaces.IDBConnection;
import pathways.util.Debug.CDebug;

public class CDBConnection implements IDBConnection 
{
  public CDBConnection() 
  {
    conn = null;
  }
  
  public void setClass(String className) throws ClassNotFoundException
  {
    try
    {
      Class.forName(className); 
    }
    catch (ClassNotFoundException e)
    {
      CDebug.println(this.getClass().getName(),"Class: " + className + " not found.");
      throw e; 
    }
  }
  
  public void setConnect(String url) throws SQLException
  {
    try
    {
      conn = DriverManager.getConnection(url);
      conn.setTransactionIsolation(Connection.TRANSACTION_READ_COMMITTED);
      //conn.setAutoCommit(false);
    }
    catch(SQLException e)
    {
      CDebug.println(this,"Unable to connect. Error: " + e.toString());
      throw e;
    }  
  }
  
  public Connection getConnection()
  {
    return conn;  
  }
  
  public ResultSet runQuery(String sqlQuery)
  {    
    ResultSet r = null;
    
    try
    {
      CDebug.println(this,"Running: " + sqlQuery);
      Statement stmt = conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY);
      r = stmt.executeQuery(sqlQuery);
      
      
      CDebug.println(this,"AutoCommit = " + conn.getAutoCommit());
      
    }
    catch(SQLException e)
    {
      CDebug.println(this,"SQL Execute error." + e.toString()); 
    }
    
    return r;  
  }
  
  public int runUpdate(String updateQuery)
  {
    int r = 0;
    
    try
    {
      CDebug.println(this,"Running Update: " + updateQuery);
      Statement stmt = conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY);
      r = stmt.executeUpdate(updateQuery);
      
      CDebug.println(this,"AutoCommit = " + conn.getAutoCommit());
    }
    catch(SQLException e)
    {
      CDebug.println(this,"SQL Update error." + e.toString()); 
    }
    
    return r;       
  }

  public void beginTrans() throws SQLException
  {
    //conn.setTransactionIsolation(Connection.TRANSACTION_READ_COMMITTED);
    conn.setAutoCommit(false);
    
    //System.out.println("Auto commit set to = " + conn.getAutoCommit());
  }
  
  public void commitTrans() throws SQLException
  {
    conn.commit();
    //conn.setAutoCommit(true);
  }
  
  public void rollbackTrans() throws SQLException
  {
    conn.rollback();
    //conn.setAutoCommit(true);  
  }
  
  public boolean isTransActive()
  {
    try{
      return conn.getAutoCommit();
    } catch (SQLException e) {
      CDebug.println(this, "isTransActive() Exception:" + e.toString());
      return false;
    }
  }
   
  private Connection conn;
}